package com.itboye.hutouben.photo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.itboye.hutouben.R;
import com.itboye.hutouben.activity.driverrepair.fragment.RepairTvFragment;
import com.itboye.hutouben.activity.luntan.TieZiWriteActivity;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.XImageLoader;

import java.util.ArrayList;
import java.util.List;

public class CopyOfPhotoActivity extends Activity {

	private ArrayList<View> listViews = null;
	private ViewPager pager;
	private MyPageAdapter adapter;
	private int count;

	public List<Bitmap> bmp = new ArrayList<Bitmap>();
	public List<String> drr = new ArrayList<String>();
	public List<String> del = new ArrayList<String>();
	public int max;
	int type = 0;
	RelativeLayout photo_relativeLayout;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo);
		type = getIntent().getIntExtra("type", 0);
		photo_relativeLayout = (RelativeLayout) findViewById(R.id.photo_relativeLayout);
		photo_relativeLayout.setBackgroundColor(0x70000000);


		if (type == 0) {
			max = RepairTvFragment.listImg.size();
		} else if (type == 1) {
			max = RepairTvFragment.listImg.size();
		}else if (type == 2) {
			max = TieZiWriteActivity.listImg.size();
		}
		Button photo_bt_exit = (Button) findViewById(R.id.photo_bt_exit);
		photo_bt_exit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setResult(103);
				finish();
			}
		});
		Button photo_bt_del = (Button) findViewById(R.id.photo_bt_del);
		photo_bt_del.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (listViews.size() == 1) {

					if (type == 0) {
						RepairTvFragment.listImg.clear();
					} else if (type == 1) {
						RepairTvFragment.listImg.clear();
					} else if (type == 2) {
						TieZiWriteActivity.listImg.clear();
					}
					// FileUtils.deleteDir();
					setResult(102);
					finish();
				} else {

					if (type == 0) {
						RepairTvFragment.listImg.remove(count);
					} else if (type == 1) {
						RepairTvFragment.listImg.remove(count);
					}else if (type == 2) {
						TieZiWriteActivity.listImg.remove(count);
					}
					max--;
					pager.removeAllViews();
					listViews.remove(count);
					adapter.setListViews(listViews);
					adapter.notifyDataSetChanged();
				}
			}
		});
		Button photo_bt_enter = (Button) findViewById(R.id.photo_bt_enter);
		photo_bt_enter.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				setResult(103);
				finish();
			}
		});

		pager = (ViewPager) findViewById(R.id.viewpager);
		pager.setOnPageChangeListener(pageChangeListener);
		if (type == 0) {
			for (int i = 0; i < RepairTvFragment.listImg.size(); i++) {
				initListViews(RepairTvFragment.listImg.get(i));//
			}
		} else if (type == 1) {
			for (int i = 0; i < RepairTvFragment.listImg.size(); i++) {
				initListViews(RepairTvFragment.listImg.get(i));//
			}
		} else if (type == 2) {
			for (int i = 0; i < TieZiWriteActivity.listImg.size(); i++) {
				initListViews(TieZiWriteActivity.listImg.get(i));//
			}
		}
		adapter = new MyPageAdapter(listViews);// 构造adapter
		pager.setAdapter(adapter);// 设置适配器
		Intent intent = getIntent();
		int id = intent.getIntExtra("ID", 0);
		pager.setCurrentItem(id);
	}

	private void initListViews(String path) {
		if (listViews == null)
			listViews = new ArrayList<View>();
		ImageView img = new ImageView(this);// 构造textView对象
		img.setBackgroundColor(0xff000000);
		try {
			Integer.parseInt(path);
			XImageLoader.load(Const.IMAGE_HEAD + path, img);
		} catch (Exception e) {
			// TODO: handle exception
			XImageLoader.load("file://" + path, img);
		}
		// XImageLoader.load(NetPublicConstant.IMAGE_URL+path, img);
		// img.setImageBitmap(bm);
		img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		listViews.add(img);// 添加view
	}

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

		public void onPageSelected(int arg0) {// 页面选择响应函数
			count = arg0;
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {// 滑动中。。。

		}

		public void onPageScrollStateChanged(int arg0) {// 滑动状态改变

		}
	};

	class MyPageAdapter extends PagerAdapter {

		private ArrayList<View> listViews;// content

		private int size;// 页数

		public MyPageAdapter(ArrayList<View> listViews) {// 构造函数
															// 初始化viewpager的时候给的一个页面
			this.listViews = listViews;
			size = listViews == null ? 0 : listViews.size();
		}

		public void setListViews(ArrayList<View> listViews) {// 自己写的一个方法用来添加数据
			this.listViews = listViews;
			size = listViews == null ? 0 : listViews.size();
		}

		public int getCount() {// 返回数量
			return size;
		}

		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		public void destroyItem(View arg0, int arg1, Object arg2) {// 销毁view对象
			((ViewPager) arg0).removeView(listViews.get(arg1 % size));
		}

		public void finishUpdate(View arg0) {
		}

		public Object instantiateItem(View arg0, int arg1) {// 返回view对象
			try {
				((ViewPager) arg0).addView(listViews.get(arg1 % size), 0);

			} catch (Exception e) {
			}
			return listViews.get(arg1 % size);
		}

		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

	}
}
